fix(bilibili): filter null subtitle content to prevent literal 'null' strings in video summaries#964
Conversation
…ings in video summaries When Bilibili subtitle API returns entries with null content values, JavaScript string concatenation converts them to the literal string "null", polluting the subtitle text sent to the AI model. Replace the manual loop with .map().filter().join() to skip null/undefined content entries before building the subtitle string. Fixes ChatGPTBox-dev#893 Co-Authored-By: Octopus <liyuan851277048@icloud.com>
Review Summary by QodoFix Bilibili null subtitle content filtering
WalkthroughsDescription• Filter null subtitle content from Bilibili API responses • Replace manual loop with functional chain for cleaner code • Prevent literal "null" strings polluting AI model input Diagramflowchart LR
A["Bilibili subtitle API response"] -->|contains null entries| B["Old: manual loop concatenation"]
B -->|produces literal 'null' strings| C["Polluted subtitle text"]
A -->|contains null entries| D["New: map-filter-join chain"]
D -->|skips null/undefined values| E["Clean subtitle text"]
File Changes1. src/content-script/site-adapters/bilibili/index.mjs
|
📝 WalkthroughWalkthroughChanges subtitle text assembly in the Bilibili site adapter from an index-based loop with manual last-element handling to a functional pipeline using map, filter, and join operations. This approach removes null and undefined values and eliminates explicit separator logic. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request refactors the subtitle concatenation logic in the Bilibili site adapter, replacing a manual loop with a functional approach using map, filter, and join. Feedback was provided to improve the robustness of this logic by adding a fallback for the subtitles array and using optional chaining to prevent potential TypeErrors from unexpected API responses.
| const subtitleContent = subtitles | ||
| .map((s) => s.content) | ||
| .filter((c) => c != null) | ||
| .join(',') |
There was a problem hiding this comment.
The current implementation assumes that subtitles is always an array and that each element s is a valid object. If the API returns an unexpected response (e.g., subtitleData.body is missing or contains null entries), this code will throw a TypeError. Adding a fallback for subtitles and using optional chaining for s.content would make this more robust.
| const subtitleContent = subtitles | |
| .map((s) => s.content) | |
| .filter((c) => c != null) | |
| .join(',') | |
| const subtitleContent = (subtitles || []) | |
| .map((s) => s?.content) | |
| .filter((c) => c != null) | |
| .join(',') |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/content-script/site-adapters/bilibili/index.mjs (1)
54-57: LGTM — clean fix for the literal "null" tokens.Using
c != nullcorrectly drops bothnullandundefinedentries, and the functional pipeline removes the manual separator handling. This directly resolves the issue described in#893.One optional consideration: empty-string
contentvalues (if the API ever returns them) would still pass the filter and produce adjacent commas (e.g.,text,,more). If you want to be defensive against that, tightening the predicate would also strip blanks:♻️ Optional: also drop empty / whitespace-only entries
const subtitleContent = subtitles .map((s) => s.content) - .filter((c) => c != null) + .filter((c) => c != null && String(c).trim() !== '') .join(',')🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/content-script/site-adapters/bilibili/index.mjs` around lines 54 - 57, Currently subtitleContent is built from subtitles.map(s => s.content).filter(c => c != null).join(','); keep the existing null/undefined guard but tighten the predicate to also exclude empty or whitespace-only strings: when mapping to content in the subtitle pipeline (variable subtitleContent and the subtitles.map callback), update the filter to require c != null and that c.trim() is not empty so blank strings are dropped before join, preventing adjacent commas in output.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/content-script/site-adapters/bilibili/index.mjs`:
- Around line 54-57: Currently subtitleContent is built from subtitles.map(s =>
s.content).filter(c => c != null).join(','); keep the existing null/undefined
guard but tighten the predicate to also exclude empty or whitespace-only
strings: when mapping to content in the subtitle pipeline (variable
subtitleContent and the subtitles.map callback), update the filter to require c
!= null and that c.trim() is not empty so blank strings are dropped before join,
preventing adjacent commas in output.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 7b8ca80e-64bb-45c0-a78a-e581df365d01
📒 Files selected for processing (1)
src/content-script/site-adapters/bilibili/index.mjs
There was a problem hiding this comment.
Pull request overview
Fixes Bilibili subtitle concatenation so null/undefined subtitle entries don’t get coerced into literal "null" strings that pollute the prompt sent to the summarization model.
Changes:
- Replaced the manual subtitle concatenation loop with a
map → filter → joinpipeline. - Filters out
null/undefinedsubtitlecontentvalues before building the subtitle string.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Fixes #893
Problem
The Bilibili subtitle API sometimes returns entries with
nullcontent values. When these are concatenated into a string using JavaScript's+operator,nullgets coerced to the literal string"null", polluting the subtitle text sent to the AI model with large amounts of the word "null".Solution
Replace the manual
forloop with.map().filter().join()to skip any subtitle entries wherecontentisnullorundefinedbefore building the subtitle string:Testing
Reproduced by simulating a subtitle response with null-content entries; confirmed the old code produces
"null,null,actual text"and the new code produces"actual text"only.Summary by CodeRabbit